package com.unity3d.app5;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.app.Fragment;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

public class MainActivity extends AppCompatActivity {

    //创建一个BlankFragment的对象
    BlankFragment blankFragment=new BlankFragment();
    MessageFragment messageFragment=new MessageFragment();
    ContactFragment contactFragment=new ContactFragment();
    DongtaiFragment2 dongtaiFragment2=new DongtaiFragment2();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imgView = findViewById(R.id.imageView);
        ImageView imgView2 = findViewById(R.id.imageView2);


        //加载本地图片

        //加载网络图片，使用第三方库
        /*
        1. 在项目的build.gradle中引入第三方库
        2. 在activity中引入网络图片
        3. 在Manifest.xml中添加网络请求权限
         */
        //显示圆形图片
        Glide.with(this).
                load("https://tse2-mm.cn.bing.net/th/id/OIP-C.rMg01-JZmU5ILr4z_PjREQHaEl?w=243&h=180&c=7&r=0&o=5&dpr=1.2&pid=1.7")//网络图片地址URL
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(imgView);
        //显示圆角图片 new RoundedCorners(30)
        Glide.with(this).
                load("https://profile.csdnimg.cn/5/2/B/1_weixin_53463734")//网络图片地址URL
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                .into(imgView2);

        //https://profile.csdnimg.cn/5/2/B/1_weixin_53463734
        /*
        Fragment简介
        出现历史
        2.3-> 3.0版本 平板电脑屏幕大的问题
              4.0版本 Fragment在手机中使用
        针对平板电脑，只需要针对屏幕的某一块区域进行切换，节省资源
        对于整个场景只使用一个Activity，只对某一区域的Fragment进行切换
         */

        ConstraintLayout button=findViewById(R.id.constraintLayout2);
        ConstraintLayout button1=findViewById(R.id.constraintLayout);
        ConstraintLayout button2=findViewById(R.id.constraintLayout3);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment1,messageFragment).commit();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment1,contactFragment).commit();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment1,dongtaiFragment2).commit();

        getSupportFragmentManager().beginTransaction()
                .show(messageFragment)
                .hide(contactFragment)
                .hide(dongtaiFragment2)
                .commit();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //replace()每调用一次都会创建一个fragment,使用add将所有的fragment加入，使用show和hide控制fragment的显示与隐藏
                //getSupportFragmentManager().beginTransaction().replace(R.id.fragment1,messageFragment).commit();
                getSupportFragmentManager().beginTransaction()
                        .show(messageFragment)
                        .hide(contactFragment)
                        .hide(dongtaiFragment2)
                        .commit();
                //设置按钮的变化，true为选中状态，false为未选中状态
                button.setSelected(true);
                button1.setSelected(false);
                button2.setSelected(false);

            }
        });

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               //getSupportFragmentManager().beginTransaction().replace(R.id.fragment1,contactFragment).commit();
                getSupportFragmentManager().beginTransaction()
                        .show(contactFragment)
                        .hide(messageFragment)
                        .hide(dongtaiFragment2)
                        .commit();
                button.setSelected(false);
                button1.setSelected(true);
                button2.setSelected(false);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //getSupportFragmentManager().beginTransaction().replace(R.id.fragment1,dongtaiFragment2).commit();
                getSupportFragmentManager().beginTransaction()
                        .show(dongtaiFragment2)
                        .hide(messageFragment)
                        .hide(contactFragment)
                        .commit();
                button.setSelected(false);
                button1.setSelected(false);
                button2.setSelected(true);
            }
        });
        //getSupportFragmentManager().beginTransaction().replace(R.id.fragment1,blankFragment).commit();
    }
}